package com.turing.post.datatranferpmbs.impl;

import java.math.BigInteger;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.turing.post.capitalaccounts.util.CapitalApplicationException;
import com.turing.post.common.util.ContrastDao;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.datatranfer.util.TranferApplicationException;
import com.turing.post.datatranferpmbs.util.OldMerchantBusiness;
import com.turing.post.datatranferpmbs.util.OldMerchantDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.Merchant;
import com.turing.post.organization.util.MerchantBranch;
import com.turing.post.organization.util.MerchantBranchBusiness;
import com.turing.post.organization.util.MerchantBusiness;
import com.turing.post.organization.util.MerchantContract;
import com.turing.post.organization.util.MerchantContractDao;
import com.turing.post.organization.util.MerchantDao;
import com.turing.post.organization.util.MerchantFeeSettlementPolicy;
import com.turing.post.organization.util.MerchantFeeSettlementPolicyDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 商户实现接口
 * 
 * @author WZD
 */
@Stateless(mappedName = "datatranferpmbs.impl.OldMerchantBusinessImpl")
@Remote
@Local
public class OldMerchantBusinessImpl implements OldMerchantBusiness {

	@EJB(mappedName = "organization.impl.MerchantBranchBusinessImpl")
	private MerchantBranchBusiness merchantBranchBusinessImpl;
	@EJB(mappedName = "datatranferpmbs.impl.OldMerchantDaoImpl")
	private OldMerchantDao oldMerchantDaoImpl;
	@EJB(mappedName = "organization.impl.MerchantBusinessImpl")
	private MerchantBusiness merchantBusinessImpl;// 新数据库商户接口

	@EJB(mappedName = "organization.impl.MerchantDaoImpl")
	private MerchantDao merchantDaoImpl;// 新数据库商户接口

	@EJB(mappedName = "common.impl.ContrastDaoImpl")
	private ContrastDao contrastDaoImpl;// 对应表接口

	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDaoImpl;// 发卡商

	@EJB(mappedName = "organization.impl.MerchantFeeSettlementPolicyDaoImpl")
	private MerchantFeeSettlementPolicyDao merchantFeeSettlementPolicyDaoImpl;// 商户结算策略

	@EJB(mappedName = "organization.impl.MerchantContractDaoImpl")
	private MerchantContractDao merchantContractDaoImpl;// 商户合同

	@EJB(mappedName = "	common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao commonDaoImpl;

	public void insertNewMerchant(PCPSWebInput webInput1,
			String issuecompanyno, String regionNo, String incomeBranchId,
			String developBranchNo) throws PCPSApplicationException {
		List<Object[]> list = oldMerchantDaoImpl.getOldMerchantList();
		Merchant merchant = null;
		Object[] obj = null;
		developBranchNo = issuecompanyno + "99";
		PCPSWebInput webInput = new PCPSWebInput();
		webInput.setOperBranchNo(issuecompanyno);
		webInput.setOperId(new BigInteger("3"));
		IssueCompany issueCompany = issueCompanyDaoImpl
				.getIssueCompanyByIssueCompanyNo(issuecompanyno);
		Date systime = commonDaoImpl.getDatabaseSysTime();

		HashMap<String, BigInteger> map = oldMerchantDaoImpl.getMerchantSett();
		for (int i = 0; i < list.size(); i++) {
			obj = list.get(i);
			// 检查发卡商机构是否存在
			if (PCPSUtil.isEmpty(issuecompanyno)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "发卡商为空!");
			}
			// 检查行政区域码否存在
			if (PCPSUtil.isEmpty(regionNo)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "行政区域码为空!");
			}
			// 检查卡种类是否存在
			if (PCPSUtil.isEmpty(incomeBranchId)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "收益机构为空!");
			}
			if (PCPSUtil.isEmpty(developBranchNo)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "开发单位机构号为空!");
			}

			String newmerchantno = merchantDaoImpl.getAutoMerchantNo(
					issuecompanyno, regionNo);
			newmerchantno = issuecompanyno + "8" + newmerchantno + regionNo;
			if (PCPSUtil.isEmpty(newmerchantno)) {
				throw new CapitalApplicationException(
						TranferApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "产生新商户号为空!");
			}
			MerchantBranch merchantbranch = null;
			MerchantContract merchantContract = null;
			MerchantFeeSettlementPolicy merchantFeeSettlementPolicy = null;
			if (merchantDaoImpl.getMerchantByMerchantNo(newmerchantno) == null) {
				merchant = new Merchant();
				merchant.setMerchantNo(newmerchantno);
				merchant.setOldMerchantNo(obj[0].toString());
				merchant.setMerchantName(obj[1].toString());
				merchant.setRegionNo(regionNo);
				merchant
						.setIncomeBranchId(new BigInteger(incomeBranchId.trim()));
				merchant.setDevelopBranchNo(developBranchNo);
				merchant.setNeedBusinessType("0");
				merchant.setLinkMan(obj[2] == null ? "" : obj[2].toString());
				merchant.setLinkTele(obj[3] == null ? "" : obj[3].toString());
				merchant.setActivate(obj[4].toString());
				merchant.setIssueCompanyNo(issuecompanyno);
				merchant.setTranDate(issueCompany.getTranDate());
				merchant.setTranStatus(obj[6].toString());
				merchant.setTranSeqNo(new BigInteger("0"));
				merchant.setBalance(obj[8] == null ? new BigInteger("0")
						: new BigInteger(obj[8].toString()));
				merchant.setCheckAccMode(obj[16].toString());
				merchant.setTranKey("00000000");
				merchant.setChannelMode("03");
				merchant.setAllowDeposit("0");
				merchant.setDepositLimitType("0");
				merchant.setDepositLimitCycle(0);
				merchant.setDepositLimit(new BigInteger("0"));
				merchant.setEachDepositLimit(new BigInteger("0"));
				merchant.setEachPaidLimit(new BigInteger("0"));
				merchant.setDayPaidTotalLimit(new BigInteger("0"));
				merchant.setDayPaidCountLimit(new BigInteger("0")); //
				// merchant.setDepositSaleBranchNo("961850986101100008");
				merchantBusinessImpl.insertMerchantInfo(webInput, merchant);
				contrastDaoImpl.insertMapMerchant(obj[0].toString(),
						newmerchantno);

				merchantbranch = new MerchantBranch();
				String merchantBranchNo = newmerchantno + regionNo
						+ String.format("%02d", i);
				merchantbranch.setMerchantBranchNo(merchantBranchNo);
				merchantbranch.setMerchantNo(newmerchantno);
				merchantbranch
						.setMerchantBranchName(obj[1].toString() + "分支机构");

				merchantbranch.setRegionNo(regionNo);
				merchantBranchBusinessImpl.insertMerchantBranchInfo(webInput,
						merchantbranch);

				// 商户合同
				merchantContract = new MerchantContract();
				BigInteger ContractId = commonDaoImpl.getUniqueIntegerKey();
				merchantContract.setContractNo("HT_" + newmerchantno);
				merchantContract.setContractId(ContractId);
				merchantContract.setMerchantNo(newmerchantno);
				merchantContract.setContractTitle(obj[1].toString() + "合同");
				merchantContract.setCreateTime(systime);
				merchantContract.setStartDate(PCPSUtil
						.getDateByString("2013-01-01 00:00:00"));
				merchantContract.setEndDate(PCPSUtil
						.getDateByString("2014-01-01 00:00:00"));
				merchantContract.setSettRule("固定比率");
				merchantContract.setSettCycle(7);
				merchantContract.setStatus("2");
				merchantContract.setPolicy("0");
				merchantContract.setFileId(new BigInteger("1"));
				merchantContract.setIssueOper("张三");
				merchantContract.setMerchantOper("李四");
				merchantContract.setOperId(webInput.getOperId());
				merchantContractDaoImpl.insert(merchantContract);

				// 策略
				merchantFeeSettlementPolicy = new MerchantFeeSettlementPolicy();
				merchantFeeSettlementPolicy.setMerchantNo(newmerchantno);
				merchantFeeSettlementPolicy.setFeePolicyName("固定比率");
				merchantFeeSettlementPolicy.setLastModifyTime(systime);
				merchantFeeSettlementPolicy.setPolicyStartDate(PCPSUtil
						.getDateByString("2013-01-01 00:00:00"));
				merchantFeeSettlementPolicy.setPolicyEndDate(PCPSUtil
						.getDateByString("2014-01-01 00:00:00"));
				merchantFeeSettlementPolicy.setPolicy("0");
				merchantFeeSettlementPolicy.setFeeRatio(map.get(merchant
						.getOldMerchantNo()));
				merchantFeeSettlementPolicy.setLimitAmount(BigInteger.ZERO);
				merchantFeeSettlementPolicy.setFloatFeeRatio(BigInteger.ZERO);
				merchantFeeSettlementPolicy.setContractId(ContractId);
				merchantFeeSettlementPolicy.setOperId(webInput.getOperId());
				merchantFeeSettlementPolicy.setStatus("1");// 状态草稿
				// 主键
				merchantFeeSettlementPolicy.setFeePolicyId(commonDaoImpl
						.getUniqueIntegerKey());
				merchantFeeSettlementPolicyDaoImpl
						.insertMerchantFeeSettlementPolicy(merchantFeeSettlementPolicy);
			}
		}
	}
}
